//
//  ZFBIndexViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-5-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ZFBIndexViewController.h"
#import "AlertView.h"
#import "GTMBase64.h"



@implementation ZFBIndexViewController

@synthesize cost, phoneNumber;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    self.title = NSLocalizedString(@"Charge3Title", @"");
    return self;
}

- (void)dealloc
{
    [cost release];
    [phoneNumber release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    cost.delegate = self;
    cost.returnKeyType = UIReturnKeyDone;
    cost.keyboardType = UIKeyboardTypeNumberPad;
    
    phoneNumber.delegate = self;
    phoneNumber.returnKeyType = UIReturnKeyDone;
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;

    [self.navigationItem setTitle:@"支付包语音支付"];
    
    app = [ELifeAppDelegate App];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (IBAction)closeKeypad:(id)sender
{
    [cost resignFirstResponder];
    [phoneNumber resignFirstResponder];
    
    [sender resignFirstResponder];
}


-(IBAction)cancel:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	// the user pressed the "Done" button, so dismiss the keyboard
	[textField resignFirstResponder];
	return YES;
}

-(IBAction)confirm:(id)sender
{
    //    [self.navigationController popViewControllerAnimated:YES];
    
    if ([cost.text length] == 0
        || [phoneNumber.text length] == 0
        ){
        [AlertView showAlert:@"请填写完整"];
        return;
    }
    
    
    [cost resignFirstResponder];
    [phoneNumber resignFirstResponder];
    
    [app showProgressDialog:@"充值中"];
    
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@mobile2/recharge?", [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil]]];
    

    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:5];
    
    NSData* namedata = [[NSString stringWithString:@"18600035966"] dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableString* retStr = [[NSMutableString alloc]  initWithData:[GTMBase64 encodeData:namedata] encoding:NSUTF8StringEncoding];
    
    [param setObject:retStr forKey:@"uname"];
    [retStr release];
    
    [param setObject:[NSString stringWithFormat:@"%i", @"10"] forKey:@"type"];
    
    NSString* content = [NSString stringWithFormat:@"%@%@", cost.text, phoneNumber.text];
    [param setObject:content forKey:@"content"];
    
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[app httpRequestPoint]openUrl:dic postParam:param target:self];
    
    [param release];
}
#pragma mark -
#pragma mark 联网回调
- (void)iRequestFinished:(NSData *)request
{
    [app hideProgressDialog];
    
    NSMutableString* retStr = [[NSMutableString alloc]  initWithData:request encoding:NSUTF8StringEncoding];
    NSLog(@"%@", retStr);
    
    if ([retStr isEqualToString:@"01"]) {
        [AlertView showAlert:@"充值成功"];
    } else {
        [AlertView showAlert:@"充值失败"];
    }
    
    [retStr release];
}

@end
